//
//  NSDictionary+KSJSON.m
//  INMOTION
//
//  Created by Kyle Sun on 9/17/15.
//  Copyright © 2015 Inmotion. All rights reserved.
//

#import "KSJSON.h"

@implementation NSDictionary (KSJSON)

- (NSString *)ks_JSONString {
    if ([NSJSONSerialization isValidJSONObject:self]) {
        NSError *error;
        NSData *registerData = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
        return [[NSString alloc] initWithData:registerData encoding:NSUTF8StringEncoding];
    }
    return nil;
}

@end

@implementation NSArray (KSJSON)

- (NSString *)ks_JSONString {
    if ([NSJSONSerialization isValidJSONObject:self]) {
        NSError *error;
        NSData *registerData = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
        return [[NSString alloc] initWithData:registerData encoding:NSUTF8StringEncoding];
    }
    return nil;
}

@end

@implementation NSString (KSJSON)

- (id)ks_objectFromJSONString {
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error) {
        NSLog(@"%@", error.description);
    }
    return result;
}

@end
